package thread;

import javax.script.ScriptContext;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:notify 和 wait
 * User:34398
 * Date:2024-07-18
 * Time:15:02
 */

//notify和wait是Object类种自带的方法
/*
 * 两个都会对线程的锁进行开锁，
 * wait会把当前线程进入waiting状态
 * 然后等待notify指令唤醒
 *
 * notify会解开当前线程的锁，然后通知被wait的线程让他唤醒。
 * */
public class Threads {
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();
    private static final Object lock3 = new Object();
    public static void main(String[] args) throws InterruptedException {
                    Thread thread1=new Thread(()->{
                      synchronized (lock1){
                          try {
                              lock1.wait();
                          } catch (InterruptedException e) {
                              System.out.println("捕获到异常");

                          }
                          System.out.println("hehe");
//                          try {
//                              lock1.wait();
//                          } catch (InterruptedException e) {
//                              e.printStackTrace();
//                          }
                      }
                    });

                    Thread thread2=new Thread(()->{
                        try {
                            Thread.sleep(1000);//确保1线程wait
                            System.out.println("thread2即将中断thread1");
                            thread1.interrupt();
                            System.out.println("thread2已经中断thread1");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    });
                thread1.start();
                thread2.start();

    }
}

